package com.wiim.ui;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.CustomGoogleMapFragment;
import com.wiim.nhapnhap.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class DiaDiemFragment extends CustomGoogleMapFragment implements OnClickListener, OnMapClickListener {
	private EditText edTimkiem;
	private Button btnDanhSach;
	private CustomFragmentActivity activity;
	private LinearLayout mapviewLinearLayout;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.diadiem, container, false);
		initFeld(view);
		// initData();
		return view;
	}

	private void initData() {
		edTimkiem.setOnClickListener(this);
		btnDanhSach.setOnClickListener(this);
		activity = (CustomFragmentActivity) getActivity();
		setMakerMap("", Utilities.getGlobalVariable(getActivity()).latlongDefault);

	}

	private void initFeld(View view) {
		btnDanhSach = (Button) view.findViewById(R.id.btnDanhSach);
		edTimkiem = (EditText) view.findViewById(R.id.btnTimkiem);
//		mapviewLinearLayout = (LinearLayout) view.findViewById(R.id.mapviewLinearLayout);

	}

	protected void setUpMapIfNeeded() {
//		showDialog(getActivity(), getActivity().getString(R.string.str_installgoogle));
		if (mMap == null) {
			mMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapMain));
			mMap = mMapFragment.getMap();
			if (mMap != null) {
				mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getCurrentLocation(), 11));
				mMap.setOnMapClickListener(this);
			} else {
				mapviewLinearLayout.setVisibility(View.GONE);
				showDialog(getActivity(), getActivity().getString(R.string.str_installgoogle));
			}
		}
	}

	private void showDialog(final Context context, final String message) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setNeutralButton(getString(R.string.str_Chapnhan), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(final DialogInterface dialog, final int id) {

				Intent intentMarket = new Intent(Intent.ACTION_VIEW, Uri.parse(ConstantValue.LINK_GOOGLE_PLAY_SERVICE));
				startActivity(intentMarket);

			}
		});
		builder.setNegativeButton(getString(R.string.str_Huy), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();

			}

		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setUpMapIfNeeded();
		if (mMap != null) {
			initData();
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		releaseMap();
	}

//	public void releaseMap() {
//		try {
//			FragmentManager manager = this.getFragmentManager();
//			FragmentTransaction trans = manager.beginTransaction();
//			SupportMapFragment frag = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapMain));
//			if (null != frag) {
//				trans.remove(frag);
//			}
//			trans.commit();
//		} catch (Throwable e) {
//			e.printStackTrace();
//		}
//	}

	@Override
	public void onClick(View v) {
		if (v == btnDanhSach) {
			activity.replaceFragment(ConstantValue.DANHSACH_FRAGMENT, false);
		}

	}

	@Override
	public void onMapClick(LatLng arg0) {
		// TODO Auto-generated method stub

	}

}
